# -*- coding: utf-8 -*-
from application import app, db
from flask import Blueprint, request, make_response, redirect
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, ops_render
from common.libs.DataHelper import getCurrentTime, getCurrentDate
from common.libs.Helper import unduplicate, handle_data, datetimetran, getage, dingtalkpost
from common.models.user import User
from common.models.department import Department
from common.models.user_change import UserChange
from common.models.user_eduexp import UserEduexp
from common.models.user_workexp import UserWorkexp
from common.models.user_family import UserFamily
from common.models.user_tag import UserTag
from common.models.user_post import UserPost
from common.models.keymap import Keymap
from common.models.user_weight import UserWeight
from common.models.user_resign import UserResign
from common.models.weight import Weight
from common.models.user_commission import UserCommission
from sqlalchemy import and_, or_
from common.libs.SQLTransform import queryToDict
from dingtalk import SecretClient, model
import json, requests
from config.dingtalk_setting import AppKey, AppSecret, AgentId, role_hr, bpmid
import os
import sys
from werkzeug.utils import secure_filename
import urllib.parse
import ast
import pandas as pd
import datacompy
from sqlalchemy import desc

##连接
client = SecretClient(AppKey, AppSecret)  # 新 access_token 获取方式

connector_page = Blueprint("connector_page", __name__)


@connector_page.route('/getcontractbody', methods=['GET', 'POST'])
def getuserbystatus():
    userid = request.json.get('userid')

    user=User.query.filter_by(userid=userid).first()
    info=str(user.contract_body)
    print(userid)

    return json.dumps({"info":info}, ensure_ascii=False)


@connector_page.route('/gettest', methods=['GET', 'POST'])
def gettest():
    token_params = {
        'appKey': 'obk_DJZNKJ',
        'appSecurity': 'xR1^}nHJ4sqb)BlUE5*0YQP(',
    }
    dd_token = 'https://ct.ctrip.com/SwitchAPI/Order/Ticket'
    token_response = requests.post(dd_token, params=token_params)  # 向API发起GET请求
    tokenData = token_response.json()
    print(tokenData)
    ticket=str(tokenData['Ticket'])

    user=User.query.filter(User.id==739).all()
    userinfo = {
    "Language": "null",
    "Appkey": "obk_DJZNKJ",
    "Ticket": ticket,
    "CorporationID": "DJZNKJ",
    "AuthenticationInfoList": []
    }
    for i in user:
        if str(i.status)=="0":
            temp = { "Sequence" : 0 ,"Authentication": {"EmployeeID": str(i.id), "Name": i.name,"ContactTel": i.mobile, "Email": i.email,"Valid":"I","SubAccountName":'DJZNKJ',"UseTRFlag":31},
                    }
        if str(i.status) == "1" or str(i.status) == "4":
            temp = {"Sequence" : 0 ,"Authentication": {"EmployeeID": str(i.id), "Name": i.name, "ContactTel": i.mobile, "Email": i.email,
                                       "Valid": "A", "SubAccountName": 'DJZNKJ', "UseTRFlag": 31},
                    }

        userinfo["AuthenticationInfoList"].append(temp)



    content=json.dumps(userinfo)
    print(content)

    url = 'https://ct.ctrip.com/corpservice/CorpCustService/SaveCorpCustInfoList'
    response = requests.post(url, params= content)  # 向API发起GET请求

    result=response.json()


    return result

